﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MedicalAdviser.DAL.Acessing
{
    public class EFUnitOfWorkFactory : IUnitOfWorkFactory
    {
        private static Func<ObjectContext> _objectContextDelegate;
        private static readonly Object _lockObject = new object();

        public EFUnitOfWorkFactory() { }


        #region IUnitOfWorkFactory Members



        public IUnitOfWork Create()
        {
            ObjectContext context;

            lock (_lockObject)
            {
                context = _objectContextDelegate();
            }


            return new EfUnitOfWork(context);
        }

        #endregion

        public static void SetObjectContext<T>(Func<T> objectContextDelegate) where T : ObjectContext
        {
            _objectContextDelegate = objectContextDelegate;
        }


    }
}
